package com.kgc.dao;

import com.kgc.entity.Building;
import com.kgc.untils.PageUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminBulidingDao extends BaseDao{
    public List<Building> selectBuilding(PageUtils pageUtils ,String name){
        List<Building> list = new ArrayList<>();
        try {
            String sql = "select * from building ";
            if(name!=null&&!"".equals(name)){
                sql += " where name like '%"+name+"%'";
            }
            sql+=" limit ?,?";
            ResultSet rs = super.getExecuteQuery(sql, pageUtils.start(), pageUtils.getPageSize());
            while (rs.next()){
                list.add(new Building(rs.getInt(1),rs.getString(2),rs.getInt(3)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }
    public int totalBuilding(String name){
        try {
            String sql = "select count(*) from  building ";
            if (name != null && !"".equals(name)) {
                sql += " where name like '%" + name + "%'";
            }
            ResultSet rs = super.getExecuteQuery(sql);
            while (rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //添加楼栋
    public int addBuilding(Building building){
        return super.getExecuteUpdate("INSERT INTO `building`(`id`, `name`, `type`) VALUES (null, ?, ?);"
                ,building.getName(),building.getType());
    }
    //删除楼栋

    public int deleteBuilding(String id){
        return super.getExecuteUpdate("delete from building where id=?",id);
    }

    public int updateBuilding(Building building) {
        String sql = "UPDATE `building` SET `name` = ?, `type` = ? WHERE `id` = ?";
        return super.getExecuteUpdate(sql, building.getName(), building.getType(), building.getId());
    }

    //查找楼栋
    public List<Building> selectBuildingName(){
        List<Building> list = new ArrayList<>();
        try {
            String sql = "select * from building ";
            ResultSet rs = super.getExecuteQuery(sql);
            while (rs.next()){
                list.add(new Building(rs.getInt(1),rs.getString(2),rs.getInt(3)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }
}
